<template>
  <div class="container">
    <div class="tabs-lb">
      <span>申请单</span>
    </div>

    <div class="nlxq">
      <div class="nlxq-left">
        <!-- 基础信息 -->
        <div class="top-info">
          <div class="group-title">
            <span>基础信息</span>
          </div>
          <div style="clear:both"></div>
          <div class="group-form">
            <el-form ref="infoForm" :model="requireForm" label-width="100px">
              <el-row :gutter="10">
                <el-col :span="12">
                  <el-form-item style="width:100%" label="使用开始时间">
                    <!-- <el-input v-model="form.filter.fbsj"></el-input> -->
                    <el-date-picker
                      style="width:100%"
                      v-model="requireForm.syksrq"
                      :picker-options="pickerOptionsStart"
                      value-format="yyyy-MM-dd"
                      type="date"
                    ></el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="使用结束时间">
                    <el-date-picker
                      style="width:100%"
                      v-model="requireForm.syjsrq"
                      value-format="yyyy-MM-dd"
                      :picker-options="pickerOptionsEnd"
                      type="date"
                    ></el-date-picker>
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row :gutter="10">
                <el-col :span="12">
                  <el-form-item label="应用系统">
                    <el-input v-model="requireForm.yyxt"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="访问地址">
                    <el-input v-model="requireForm.dyryip"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row>
                <el-col :span="24">
                  <el-form-item label="申请理由">
                    <el-input
                      type="textarea"
                      :rows="6"
                      placeholder="请输入申请理由"
                      v-model="requireForm.sqly"
                    ></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </div>
        <!-- 能力列表 -->
        <div class="bottom-info">
          <div class="group-title">
            <span>能力列表</span>
            <button class="btn_operation_add" @click="addOpenDialog">
              <img src="~@/assets/img/grzx/add.png" alt />
              添加
            </button>
          </div>
          <div class="bottom-table">
            <el-table
              :data="tableData"
              style="min-height:550px"
              border
              stripe
              row-key="jkbh"
              @selection-change="handleSelectionChange"
            >
              <el-table-column type="selection" align="center"></el-table-column>
              <el-table-column
                prop="jkmc"
                label="能力名称"
                align="center"
                :show-overflow-tooltip="true"
              ></el-table-column>
              <el-table-column prop="jkms" label="概述" align="center" :show-overflow-tooltip="true"></el-table-column>
              <el-table-column
                prop="fbdw"
                label="发布单位"
                align="center"
                :show-overflow-tooltip="true"
              ></el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-link type="primary" @click="deleteRow(scope.row.jkbh)">移除</el-link>
                </template>
              </el-table-column>
            </el-table>
            <div class="start-initiate">
              <el-button type="primary" @click="fqsp" :loading="loading">发起申请</el-button>
            </div>
          </div>
        </div>
      </div>
      <!-- 审批流程 -->
      <!-- <div class="nlxq-right">
        标题
        <div class="group-title">
          <span>审批流程</span>
        </div>
        <div style="clear:both"></div>
        流程
        <div class="flow-path">
          <div class="position">
            <img src="~@/assets/img/grzx/定位.png" alt />
          </div>
          可能会循环数据
          <div style="overflow: auto; height:800px">
            <div v-for="(item,index) in flowPathList" :key="index" class="path-data">
              <div class="position">
              <img src="~@/assets/img/grzx/定位.png" alt />
              </div>
              <div class="path-time">
                <div class="time-radius"></div>
                <div class="time-data">{{item.timeData}}</div>
              </div>
              <div class="path-box">
                <div class="path-item">
                  <div class="path-person">
                    <div class="person-name">{{item.name}}</div>
                    <div class="person-yard">{{item.jcYard}}</div>
                  </div>
                  <div v-if="item.sqNumber === 1" class="color-2f9bff">发起审批</div>
                  <div v-if="item.sqNumber === 2" class="color-26c55a">已同意</div>
                  <div v-if="item.sqNumber === 3" class="color-2eafdf">待审批</div>
                </div>
              </div>
            </div>
          </div>
        </div>
        底部按钮
        <div class="buttton-position">
          <el-button type="primary" @click="fqsp" :loading="loading">发起审批</el-button>
          <el-button type="primary">作废</el-button>
        </div>
      </div>-->
    </div>

    <!-- 添加能力弹窗 -->
    <addNlDialog ref="addNlDialog" @updateTable="updateTable"></addNlDialog>
  </div>
</template>
<script>
import dyglService from '@/api/dygl'
import jkglService from '@/api/jkgl'
// import nlCard from '@/components/nl-card.vue'
import addNlDialog from './dialogs/add-nl-dialog.vue'
export default {
  components: {
    // nlCard,
    addNlDialog
  },
  data() {
    return {
      // 选择要申请数据
      sqTableSelect: [],
      // 可申请数据
      tableData: [],
      // 审批流程数据
      flowPathList: [
        {
          timeData: '2020-01-12 09:22:57',
          name: '周千秋',
          jcYard: '上海市人民检查医院',
          sqNumber: 1,
        },
        {
          timeData: '2021-02-14 09:22:57',
          name: '吴真',
          jcYard: '上海市人民检查医院',
          sqNumber: 2,
        },
        {
          timeData: '2022-03-24 09:22:57',
          name: '张鹏',
          jcYard: '黄浦区人名检察院',
          sqNumber: 3,
        },
      ],
      requireForm: {
        // 使用开始日期
        syksrq: '',
        // 使用结束日期
        syjsrq: '',
        // 应用系统
        yyxt: '',
        // 访问地址
        dyryip: '',
        // 申请理由
        sqly: '',
      },
      form: {
        pageNum: 1,
        pageSize: 10,
        filter: {
          jkmc: ''
        }
      },
      total: 0,
      loading: false,
      jkbh: '',
      pickerOptionsStart: {
        disabledDate: (time) => {
          let endDateVal = this.requireForm.syjsrq;
          if (endDateVal) {
            return (
              time.getTime() > new Date(endDateVal).getTime()
            );
          }
        },
      },
      pickerOptionsEnd: {
        disabledDate: (time) => {
          let beginDateVal = this.requireForm.syksrq
          if (beginDateVal) {
            return (
              time.getTime() <
              new Date(beginDateVal).getTime() - 1 * 24 * 60 * 60 * 1000
            );
          }
        },
      },
    }
  },
  mounted() {
    //获取路由信息
    this.getRouter()
  },
  methods: {
    /* 获取路由信息 */
    getRouter() {
      if (this.$route.query.cs) {
        this.jkbh = this.$route.query.cs
        // 默认选择点击的接口
        this.getNlxq()
      }

      // else {
      //   // 分页
      //   this.changePageNum(1)
      // }
    },
    /*
     * 获取能力详情(修改)
     */
    getNlxq() {
      const params = {
        jkbh: this.jkbh
      }
      jkglService
        .getJkdy(params)
        .then(res => {
          // this.sqTableData.push(res)
          this.tableData = [];
          this.tableData.push(res);
        })
        .catch(err => {
          !err.isError &&
            this.$message({ type: 'warning', message: err.message })
        })
    },
    /*
     * 获取可申请能力列表
     */
    getSqList() {
      const params = { ...this.form }
      dyglService
        .queryKdynl(params)
        .then(res => {
          this.tableData = [];
          this.tableData = res.list
          this.total = res.total
        })
        .catch(err => {
          !err.isError &&
            this.$message({ type: 'warning', message: err.message })
        })
    },

    /*
     * 分页
     */
    changePageNum(val) {
      this.form.pageNum = val
      this.getSqList()
    },

    /*
     * 分页
     */
    changePageSize(val) {
      this.form.pageSize = val
      this.form.pageNum = 1
      this.getSqList()
    },

    //删除
    deleteRow(row) {
      this.$confirm(
        `请确认是否删除？`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        // const params = {
        //   jkbh: row.jkbh
        // };
        // jkglService
        //   .deleteJkdy(params)
        //   .then((res) => {
        //     this.$message({
        //       type: "success",
        //       message: "删除成功!",
        //     });
        //     this.getFbList();
        //   })
        //   .catch((err) => {
        //     !err.isError &&
        //       this.$message({ type: "warning", message: err.message });
        //   });
        this.tableData = this.tableData.filter(item => {
          return item.jkbh !== row
        })
        this.$message({
          type: "success",
          message: "删除成功!",
        });
      })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },

    handleSelectionChange(val) {
      this.sqTableSelect = val;
    },

    // 点击添加按钮
    addOpenDialog() {
      this.$refs.addNlDialog.openDialog(this.tableData)
    },
    // 点击发起审批
    fqsp() {
      if (!this.requireForm.syksrq) {
        this.$message.warning('请输入使用开始时间')
        return
      }
      if (!this.requireForm.syjsrq) {
        this.$message.warning('请输入使用结束日期')
        return
      }
      if (!this.requireForm.yyxt) {
        this.$message.warning('请输入应用系统')
        return
      }
      if (!this.requireForm.dyryip) {
        this.$message.warning('请输入访问地址')
        return
      }
      if (!this.requireForm.sqly) {
        this.$message.warning('请输入申请理由')
        return
      }
      if (!this.sqTableSelect.length) {
        this.$message.warning('请选择要申请的能力')
        return
      }

      const params = {
        ...this.requireForm,
        jkbhList: this.sqTableSelect.map(el => el.jkbh)
      }
      this.loading = true
      dyglService
        .addNldy(params)
        .then(res => {
          this.loading = false
          this.$message.success('申请成功')
          this.$router.go(-1)
        })
        .catch(err => {
          this.loading = false
          !err.isError &&
            this.$message({ type: 'warning', message: err.message })
        })
    },

    // 子组件穿过来的数据
    updateTable(val) {
      this.tableData = val
    }

    /*
     * 解析是否显示批量
     */
    // parseIsBatchFun(list) {
    //   return list?.length > 1 ? '(批量)' : ''
    // }
  }
}
</script>
<style lang="scss" scoped>
.container {
  width: 100%;
  .nlxq {
    margin-top: 20px;
    width: 100%;
    // height: 800px;
    display: flex;
    padding: 20px;
    background: #fff;
    border: 1px solid #cecece;

    .nlxq-left {
      width: 100%;
      height: 100%;
      border: 1px solid #cecece;
      padding: 10px;

      .bottom-info {
        margin-top: 15px;
      }

      .group-title {
        width: 100%;
        height: 40px;
        float: left;
        line-height: 40px;
        border-bottom: 1px solid #f5f5f5;
        margin-bottom: 10px;
        span {
          position: relative;
          margin-left: 15px;
          color: #333333;
          font-weight: bold;
        }
        span:before {
          content: "";
          position: absolute;
          top: 3px;
          left: -9px;
          width: 4px;
          height: 18px;
          background-color: #0e69ff;
        }
      }

      .group-form {
        padding-top: 5px;
      }
    }

    .nlxq-right {
      position: relative;
      width: 30%;
      // min-height: 800px;
      margin-left: 10px;
      border: 1px solid transparent;
      padding: 10px;

      .group-title {
        width: 100%;
        height: 40px;
        float: left;
        line-height: 40px;
        border-bottom: 1px solid #f5f5f5;
        margin-bottom: 10px;
        span {
          position: relative;
          margin-left: 15px;
          color: #333333;
          font-weight: bold;
        }
        span:before {
          content: "";
          position: absolute;
          top: 3px;
          left: -9px;
          width: 4px;
          height: 18px;
          background-color: #0e69ff;
        }
      }

      .flow-path {
        position: relative;
        .path-data {
          margin-top: 5px;
          .path-time {
            display: flex;
            align-items: center;
            padding-left: 2px;
            .time-radius {
              width: 15px;
              height: 15px;
              border: 3px solid #cecece;
              border-radius: 50%;
            }
            .time-data {
              margin-left: 13px;
              color: #9d9d9d;
            }
          }

          .path-box {
            padding-left: 30px;
            .path-item {
              display: flex;
              justify-content: space-between;
              align-items: center;
              padding: 15px;
              border: 1px solid #cecece;
              .person-name {
                color: #000;
                font-weight: 700;
              }
              .person-yard {
                color: #9d9d9d;
              }

              .color-2f9bff {
                color: #2f9bff;
              }
              .color-26c55a {
                color: #26c55a;
              }
              .color-2eafdf {
                color: #2eafdf;
              }
            }
          }
        }

        .path-data + .path-data {
          margin-top: 30px;
        }

        &::before {
          position: absolute;
          content: "";
          width: 2px;
          height: 90%;
          top: 40px;
          left: 8px;
          background-color: #cecece;
          z-index: -1;
        }
      }

      .buttton-position {
        width: 100%;
        text-align: right;
        position: absolute;

        bottom: 10px;
        padding-right: 20px;
      }
    }
  }
}

.btn_operation_add {
  margin-top: 5px;
  line-height: 0px;
}

.start-initiate {
  text-align: right;
  padding-right: 10px;
  padding-bottom: 10px;
}
</style>
